<?php

namespace backend\models;

use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use backend\models\BuildCity;
use common\models\City;
use backend\models\OperatorRelation;

/**
 * BuildCitySearch represents the model behind the search form about `backend\models\BuildCity`.
 */
class BuildCitySearch extends BuildCity
{
    public function attributes()
    {
        // add related fields to searchable attributes
        return array_merge(parent::attributes(), ['cityName.name']);
    }
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['id', 'city_id', 'sort'], 'integer'],
            ['cityName.name','string'],
            [['name', 'search_index'], 'safe'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function scenarios()
    {
        // bypass scenarios() implementation in the parent class
        return Model::scenarios();
    }

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    public function search($params)
    {
    	$login_id = Yii::$app->user->identity->id;
    	$admin_ids = AdminInfo::get_all_admin_by_operator($login_id);
    	$user_type = Yii::$app->user->identity->user_type;
		//$city = strstr(Yii::$app->user->identity->user_address, ' ');
		 //$city_id = City::get_id(trim($city));
    	if($user_type != 1){//非后台管理员
    		if(\Yii::$app->user->can('yys-view-platform')){//平台的
    		    $OperatorRelation=new OperatorRelation();
    		    $uid=Yii::$app->user->identity->user_id;
    		    $citylist_Str=$OperatorRelation->getCityIdById($uid);
    		    if($citylist_Str!=""){
    		        $conditions = "bulid_city.city_id in ({$citylist_Str}) OR bulid_city.admin_id in ($admin_ids)";
    		    }else{
    		        $conditions = " bulid_city.admin_id in ($admin_ids)";
    		    }
    			
    		}elseif(\Yii::$app->user->can('yys-view-all')){//运营商的  			
    			$conditions = "bulid_city.admin_id in ($admin_ids)";
    		}elseif(\Yii::$app->user->can('yys-view-own')){//自己的
    			$conditions = "bulid_city.admin_id={$login_id}";
    		}else{
    			$conditions = '1 <> 1';
    		}
    	}else{//后台管理员
    		$conditions = '1=1';
    	}
    	
        $query = BuildCity::find()->joinWith(['cityName'])->where($conditions)->orderBy("bulid_city.id desc");

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
            'pagination'=>['pageSize' => '20'],
        ]);
        $dataProvider->sort->attributes['cityName.name'] = [
            'asc' => ['grp_city.name' => SORT_ASC],
            'desc' => ['grp_city.name' => SORT_DESC],
        ];

        $this->load($params);

        if (!$this->validate()) {
            // uncomment the following line if you do not want to return any records when validation fails
            // $query->where('0=1');
            return $dataProvider;
        }
        $query->andFilterWhere([
            'bulid_city.id' => $this->id,
            'bulid_city.sort' => $this->sort,
        ]);

        $query->andFilterWhere(['like', 'bulid_city.name', $this->name])
        ->andFilterWhere(['like', 'grp_city.name', $this->getAttribute('cityName.name')]);
		
        return $dataProvider;
    }
}
